import {
  get,
  post,
  post_json
} from '@/http/axios'
import _ from 'lodash'
export default {
  namespaced: true,
  state: {
    //声明一个购物车
    orderLines: new Map(),
    //
    vuex_productCategoryId:'',

    prouductData_shopcar:[],


  },
  getters: {
    // 计算订单总金额
    total(state) {
      // 初始化价格          
      let result = 0;
      for (let orderLine of state.orderLines.values()) {
        result += (orderLine.price * orderLine.num)
      }
      return result;
    },

    badge(state) {
      let i = 0
      for (let orderLine of state.orderLines.values()) {
        
          i += orderLine.num
              }
      return i;
    },
    myProducts(state){
      return state.prouductData_shopcar.filter(item => item.productCategoryId == state.vuex_productCategoryId)
    }
  },
  mutations: {
    addShopCar(state, orderLine) {
      //相当于往购物车里添加订单
      state.orderLines.set(orderLine.productId, orderLine)
      let temp = new Map();
  
      for(let orderLine of state.orderLines.values()){
        if(orderLine.num != 0){
          temp.set(orderLine.productId, orderLine)
        }
      }
      //vue监听不到购物车发生变化,所以要引人lodash
      state.orderLines = _.clone(temp)
    },
   // 清空购物车
    // clearShopcar(state) {
    //   state.orderLines.clear();
    //   state.orderLines = _.clone(state.orderLines)
    // },
    SET_PRODUCTDATA(state, data){
      state.productData = data;
    },
    SET_vuex_productCategoryId(state, data){
        state.vuex_productCategoryId = data;
    }
  },
  actions: {

  }
}